home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: xara.net!SoNet!uknet!owl-uk!news
- From: Kenn@owl-uk.co.uk (Ken Nicolson)
- Subject: Re: rand ?
- Message-ID: <314da47e.28351406@hector>
- Sender: news@owl-uk.co.uk (News system)
- Organization: Office Workstations Limited
- X-Newsreader: Forte Agent .99d/32.182
- References: <4idjt4$4du@itsop2.its.brooklyn.cuny.edu> <314BB352.2734@ccis.com>
- Date: Mon, 18 Mar 1996 18:03:50 GMT
-
- Derek Lund <wlund@ccis.com> wrote:
-
- >Daniel Zielinski wrote:
- >>
- >> I need to generate random numbers between 0 and 51, I tried rand() with srand(i)
- >> i changes constantly but the program never exits. looks like the random number
- >> is always the same. any help would be apriciated... thanss
- > If you want a truely random number use something like this
- >
- >#define RANDOMIZE() srand(time(NULL)) /* time() is in time.h */
- >main()
- >{
- > int i ;
- > while(i != 0 )
- > printf("%d\n",i = random(52));
- >}
- >
- > int random(int i)
- > {
- > double x = RAND_MAX + 1.0 ;/* RAND_MAX is defined in stdlib.h */
- > int y ;
- > RANDOMIZE();
- > y = 1 + rand() * (i /x) ;
- > return y ;
- > }
- >
- >Derek Lund
- Did you even bother to try compiling and running this? It is patently
- obvious that this will not work if you call random() more than once per
- second. Maybe you should try reading the FAQ too?
-
- Ken
-